package com.kai.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName: Mabatis
 * @description：
 * @author：TanKai
 * @date：2021/6/17 17:19
 * @versions：v1.0
 */

public class Mabatis {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        String resource = "mybatis.xml";
        try {
            //第一步使用Mybatis获取SqlSessionFactory对象
            InputStream inputStream = Resources.getResourceAsStream(resource);
             sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //sql命令所需的所有方法
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}
